package com.ants.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;

/**
 * ElasticSearch 客户端配置
 *
 * @author geng
 * 2020/12/19
 */
@Configuration
public class ESRestClientConfig {


    @Value(value = "${ants.elasticsearch.cluster-nodes}")
    private String clusterNodes;
    @Value(value = "${ants.elasticsearch.username}")
    private String username;
    @Value(value = "${ants.elasticsearch.password}")
    private String password;

    //注入IOC容器
    @Bean
    public ElasticsearchClient restHighLevelClient() {

//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(username, password));
//
//        RestClient client = RestClient.builder(new HttpHost(clusterNodes, 9200,"http")).setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//            @Override
//            public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
//                return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//            }
//        }).build();
//        ElasticsearchTransport transport = new RestClientTransport(client,new JacksonJsonpMapper());
//        return new ElasticsearchClient(transport);

        ElasticsearchClient client = new ElasticsearchClient(null);
        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
        //设置账号密码
        credentialsProvider.setCredentials(
                AuthScope.ANY, new UsernamePasswordCredentials(username, password));

//        RestClients restClients =
        RestClient restClient = RestClient.builder(new HttpHost(clusterNodes, 9200))
                .setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)).build();

        ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());
        // And create the API client
        client = new ElasticsearchClient(transport);
        return client;

    }

//    @Bean
//    public RestHighLevelClient restHighLevelClient() {
//
//        // 腾讯云连接方式 start
//        final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
//        credentialsProvider.setCredentials(AuthScope.ANY,
//                new UsernamePasswordCredentials(username, password));
////
//        RestClientBuilder builder = RestClient.builder(
//                        new HttpHost(clusterNodes, 9200, "http"))
//                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
//                    @Override
//                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpAsyncClientBuilder) {
//                        return httpAsyncClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
//                    }
//                });
//        return new RestHighLevelClient(builder);
//
//        // 腾讯云连接方式 end
//
//
//        // 阿里云连接方式 start
////        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()
////                .connectedTo(clusterNodes).withBasicAuth(username,password)
////                .build();
////
////        return RestClients.create(clientConfiguration).rest();
//        // 阿里云连接方式 end
//    }
}

